package br.com.vpsa.previdordevendas.model.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.ParseException;
import java.util.Locale;

public class MoedaUtil {

	public static final DecimalFormatSymbols DECIMAL_FORMAT_SYMBOLS = new DecimalFormatSymbols (new Locale ("pt", "BR")); 
	public static final DecimalFormat MOEDA_FORMAT = new DecimalFormat("#,###,##0.00",DECIMAL_FORMAT_SYMBOLS);
	
	public static String convert( BigDecimal valor ){
		if( valor == null ){
			return null;
		}
		return MOEDA_FORMAT.format(valor);
	}
	
	public static BigDecimal convert( String valor ){
		try {
			return new BigDecimal( MOEDA_FORMAT.parse(valor).toString() );
		} catch (Exception e) {
			return null;
		}
	}
	
	
	public static void main(String[] args) throws ParseException {
		System.out.println( MOEDA_FORMAT.parse("300.000,01") );
		System.out.println( MOEDA_FORMAT.format( new BigDecimal("300000.001")) );
	}
}
